package com.scm.web.controller.purchase;


import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.core.page.TableSupport;
import com.scm.common.core.text.Convert;
import com.scm.common.utils.StringUtils;
import com.scm.dto.req.supply.ScmSupplyQuotationReq;
import com.scm.dto.resp.invent.QueryInventoryInPageResp;
import com.scm.dto.resp.product.ScmProductDo;
import com.scm.dto.resp.supply.ScmSupplyQuotationDetailResp;
import com.scm.manager.ScmSupplyQuotationManager;
import com.scm.persist.model.ScmInventoryIn;
import com.scm.persist.model.ScmSupplyQuotation;
import com.scm.service.IScmSupplyQuotationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.scm.common.annotation.Log;
import com.scm.common.enums.BusinessType;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.common.core.page.TableDataInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 询价单Controller
 *
 * @author reptilemanager
 * @date 2024-05-17
 */
@Controller
@RequestMapping("/system/quotation")
public class ScmSupplyQuotationController extends BaseController
{
    private String prefix = "quotation";

    @Autowired
    private IScmSupplyQuotationService scmSupplyQuotationService;

    @Autowired
    private ScmSupplyQuotationManager scmSupplyQuotationManager;

    @RequiresPermissions("system:quotation:view")
    @GetMapping()
    public String quotation()
    {
        return prefix + "/quotation";
    }

    @RequiresPermissions("system:quotation:view")
    @GetMapping("quotationListPage")
    public String quotationListPage()
    {
        return prefix + "/quotationListPage";
    }
    @RequiresPermissions("system:quotation:view")
    @PostMapping("quotationList")
    @ResponseBody
    public TableDataInfo quotationList(ScmSupplyQuotation scmSupplyQuotation)
    {
        SysUser sysUser = getSysUser();
        Long tenantId = sysUser.getTenantId();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        List<ScmSupplyQuotation> list = scmSupplyQuotationService.selectScmSupplyQuotationList(tenantId,scmSupplyQuotation,pageDomain);
        return getDataTable(list);
    }

    /**
     * 查询询价单列表
     */
    @RequiresPermissions("system:quotation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmSupplyQuotation scmSupplyQuotation)
    {
        startPage();
        List<ScmSupplyQuotation> list = scmSupplyQuotationService.selectScmSupplyQuotationList(scmSupplyQuotation,getSysUser());
        return getDataTable(list);
    }

    /**
     * 导出询价单列表
     */
    @RequiresPermissions("system:quotation:export")
    @Log(title = "询价单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmSupplyQuotation scmSupplyQuotation)
    {
        List<ScmSupplyQuotation> list = scmSupplyQuotationService.selectScmSupplyQuotationList(scmSupplyQuotation,getSysUser());
        ExcelUtil<ScmSupplyQuotation> util = new ExcelUtil<ScmSupplyQuotation>(ScmSupplyQuotation.class);
        return util.exportExcel(list, "询价单数据");
    }

    /**
     * 新增询价单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存询价单
     */
    @RequiresPermissions("system:quotation:add")
    @Log(title = "询价单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody ScmSupplyQuotationReq scmSupplyQuotationReq)
    {
        SysUser sysUser = getSysUser();
        return toAjax(scmSupplyQuotationManager.addSupplyQuotation(sysUser,scmSupplyQuotationReq));
    }

    /**
     * 修改询价单
     */
    @RequiresPermissions("system:quotation:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmSupplyQuotation scmSupplyQuotation = scmSupplyQuotationService.selectScmSupplyQuotationById(id);
        mmap.put("scmSupplyQuotation", scmSupplyQuotation);
        return prefix + "/edit";
    }

    /**
     * 修改保存询价单
     */
    @RequiresPermissions("system:quotation:edit")
    @Log(title = "询价单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody ScmSupplyQuotationReq scmSupplyQuotationReq)
    {
        return toAjax(scmSupplyQuotationManager.updataSupplyQuotation(scmSupplyQuotationReq));
    }

    /**
     * 删除询价单
     */
    @RequiresPermissions("system:quotation:remove")
    @Log(title = "询价单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if(StringUtils.isBlank(ids)){

            return toAjax(0);
        }

        String[] strArray = Convert.toStrArray(ids);
        List<Long> idlist = new ArrayList<>();
        for (String s : strArray) {
            idlist.add(Long.valueOf(s));
        }
        return toAjax(scmSupplyQuotationService.deleteScmSupplyQuotationByIds(idlist));
    }

    /**
     * 查询库存-入库信息列表
     */
    @RequiresPermissions("system:in:list")
    @PostMapping("/detail/list")
    @ResponseBody
    public TableDataInfo deatillist(Long id)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;
        /**
         * private String productCode;
         *
         *     private String productName;
         *
         *     private String productUnit;
         *
         *     private String productSpec;
         *
         *     private Integer productCount;
         *
         *     private String price;
         *
         *     private String remark;
         *
         *     private String taxRate;
         *
         *     private String discountRate;
         *
         *     private String amt;
         */
        List<ScmSupplyQuotationDetailResp> scmSupplyQuotationDetailResps = scmSupplyQuotationManager.detailListByScmSupplyQuotation(id);
        return getDataTable(scmSupplyQuotationDetailResps);
    }

    /**
     * 获取报价表下的全部 商品信息
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("id") Long id)
    {
        List<ScmSupplyQuotationDetailResp> scmSupplyQuotationDetailResps = scmSupplyQuotationManager.detailListByScmSupplyQuotation(id);
        //ScmProject scmProject = scmProjectService.selectScmProjectById(sysUser.getTenantId(), id);
        return AjaxResult.success(scmSupplyQuotationDetailResps);

    }


}
